﻿using System.Collections.Generic;
using UnityEngine;
namespace HBFramework
{
    /// <summary>
    /// 音频管理
    /// </summary>
    public partial class AudioManager : Singleton<AudioManager>, ISingleton, ISingletonUpdate
    {
        public int SoundVolume
        {
            get
            {
                return soundVolume;
            }
            set
            {
                soundVolume = value;
                RefreshVolume();
            }
        }
        private int soundVolume = 100;

        public int MusicVolume
        {
            get
            {
                return musicVolume;
            }
            set
            {
                musicVolume = value;
                RefreshVolume();
            }
        }
        private int musicVolume = 100;

        private ObjectPool<AudioPlayer> playerPool;
        private List<AudioPlayer> playingPlayerList;

        public int Order => 1;

        public void OnInit()
        {
            playerPool = new ObjectPool<AudioPlayer>();
            playingPlayerList = new List<AudioPlayer>();
        }

        public void OnUpdate()
        {
            for (int i = 0; i < playingPlayerList.Count; i++)
            {
                if (!playingPlayerList[i].IsPlaying())
                {
                    playerPool.RecycleObject(playingPlayerList[i]);
                    playingPlayerList.RemoveAt(i);
                    i--;
                }
            }
        }

        public void OnDestroy()
        {
            playerPool.ReleaseAll();
        }

        private void RefreshVolume()
        {
            for (int i = 0; i < playingPlayerList.Count; i++)
            {
                playingPlayerList[i].RefreshVolume();
            }
        }

        /// <summary>
        /// 播放音效
        /// </summary>
        /// <param name="clip"></param>
        /// <param name="volume"></param>
        /// <returns></returns>
        public AudioPlayer PlaySound(AudioClip audioClip, float volume)
        {
            volume = volume * soundVolume / 100f;
            var audioPlayer = playerPool.RentObject();
            audioPlayer.playerType = AudioPlayerType.Sound;
            audioPlayer.Play(audioClip, volume, false);
            playingPlayerList.Add(audioPlayer);
            return audioPlayer;
        }

        /// <summary>
        /// 播放音乐
        /// </summary>
        /// <param name="clip"></param>
        /// <param name="volume"></param>
        /// <param name="fadeTime"></param>
        /// <returns></returns>
        public AudioPlayer PlayMusic(AudioClip audioClip, float volume)
        {
            volume = volume * soundVolume / 100f;
            var audioPlayer = playerPool.RentObject();
            audioPlayer.playerType = AudioPlayerType.Music;
            audioPlayer.Play(audioClip, volume, true);
            playingPlayerList.Add(audioPlayer);
            return audioPlayer;
        }

        /// <summary>
        /// 停止播放器
        /// </summary>
        /// <param name="player"></param>
        public void StopPlayer(AudioPlayer player)
        {
            playerPool.RecycleObject(player);
        }
    }
}